package client.gui;

import java.awt.Dimension;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.util.ArrayList;
import java.util.Observable;
import java.util.Observer;

import javax.swing.BorderFactory;
import javax.swing.JButton;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextArea;
import javax.swing.JTextField;
import javax.swing.border.EtchedBorder;

import client.network.MessageMailBox;

public class Chat extends JPanel implements Observer, ActionListener {
	/**
	 * 
	 */
	private static final long serialVersionUID = -8456838214327367205L;
	private ArrayList<String> messages;
	private JTextArea history;
	private int historyRows = 6;
	private JTextField message;
	private JButton send;
	JScrollPane scroll;
	MessageMailBox mailBox;

	public Chat(int width, int height, MessageMailBox mailBox) {
		this.mailBox = mailBox;
		messages = new ArrayList<String>();
		setLayout(new GridBagLayout());
		GridBagConstraints c = new GridBagConstraints();

		history = new JTextArea();
		history.setPreferredSize(new Dimension(width, height));
		history.setRows(historyRows);
		history.setEditable(false);
		c.gridwidth = 2;
		add(history, c);

		message = new JTextField();
		message.setPreferredSize(new Dimension(width - 80, 50));
		message.addActionListener(this);
		c.gridy = 1;
		c.gridwidth = 1;
		add(message, c);

		send = new JButton("Send");
		send.addActionListener(this);
		c.gridx = 1;
		add(send, c);

		setBorder(BorderFactory.createTitledBorder(
				BorderFactory.createEtchedBorder(EtchedBorder.LOWERED), "Chat"));

		disableChat();

	}

	public void addMessage(String mess) {
		messages.add(mess);
		updateHistoryBox();
	}

	public void updateHistoryBox() {
		String text = "";
		if (messages.size() <= historyRows) {
			for (String s : messages) {
				text = text + s + "\n";
			}
		} else {
			for (int i = messages.size() - historyRows; i < messages.size(); i++) {
				text = text + messages.get(i) + "\n";
			}
		}
		if (!text.trim().startsWith("!")) {
			history.setText(text);
		} else if (text.trim().startsWith("!") && text.length() == 1)
			;
		{
			// försök att stoppe utropstecknet!
		}
	}

	public void emptyBox() {
		messages.clear();
		updateHistoryBox();
	}

	@Override
	public void update(Observable o, Object arg) {
		if (arg instanceof String) {
			String s = (String) arg;
			if (!s.startsWith("Congratulation") && !s.startsWith("#")
					&& s.charAt(0) != '=' && s.charAt(0) != '!') {
				messages.add((String) arg);
				updateHistoryBox();
			}
		}
	}

	@Override
	public void actionPerformed(ActionEvent e) {
		if (e.getSource() == send || e.getSource() == message) {
			String temp = message.getText();
			message.setText("");
			mailBox.addOutgoingMessage(temp);
		}
	}

	public void enableChat() {
		this.setEnabled(true);
		message.setEditable(true);
		send.setEnabled(true);
		history.setText("Game has started");
	}

	public void disableChat() {
		this.setEnabled(false);
		message.setEditable(false);
		send.setEnabled(false);
		history.setText("Waiting for game to start");
	}

}
